package com.example.ui;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Context context = MainActivity.this;
    private EditText editText;
    private ImageView image;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.et1);
        image = findViewById(R.id.image1);
        progressBar = findViewById(R.id.ProgressBar1);

        findViewById(R.id.btn1).setOnClickListener(this::todo1);
        findViewById(R.id.btn2).setOnClickListener(this::todo2);
        findViewById(R.id.btn3).setOnClickListener(this::todo3);
        findViewById(R.id.btn4).setOnClickListener(this::todo4);
    }

    private void todo4(View view) {
        Intent intent = new Intent(context,CustomLayoutActivity.class);
        startActivity(intent);
    }

    private void todo3(View view) {
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setTitle("This is a ProgressDialog");
        dialog.setMessage("Loading...");
        dialog.setCancelable(true); //是否可以点返回键取消
        dialog.show();

    }

    private void todo2(View view) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle("This is a AlertDialog");
        dialog.setMessage("Something import. ");
        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //click ok event
            }
        });
        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //click Cancel event
            }
        });
        dialog.show();
    }

    private void todo1(View view) {
        //EditText
        String inputText = editText.getText().toString();
        Toast.makeText(context, inputText, Toast.LENGTH_SHORT).show();

        //ImageView
        image.setImageResource(R.drawable.file2);

        //ProgressBar
        progressBar.setProgress(progressBar.getProgress() + 10);
    }
}